package com.example.esquel_carpool.ui.detail;

import android.content.Context;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.example.esquel_carpool.R;
import com.example.esquel_carpool.entity.detail.ChildCommentsBean;
import com.example.esquel_carpool.entity.detail.DetailListBean;
import com.example.esquel_carpool.entity.detail.DetailUserBeanX;
import com.example.esquel_carpool.entity.detail.UserBean;
import com.example.esquel_carpool.glideModule.GlideApp;

import java.util.ArrayList;
import java.util.List;

public class DetailListAdapter extends BaseExpandableListAdapter {

    private SparseArray<List<ChildCommentsBean>> childMap = new SparseArray<>();

    private List<DetailListBean> groupList = new ArrayList<>();

    private Context context;

    private LayoutInflater inflater;

    public DetailListAdapter(SparseArray<List<ChildCommentsBean>> childMap, List<DetailListBean> groupList, Context context) {
        if (childMap != null) {
            this.childMap = childMap;
        }
        if (groupList != null) {
            this.groupList = groupList;
        }
        this.context = context;

        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getGroupCount() {
        return groupList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        List<ChildCommentsBean> beans = childMap.get(groupPosition);
        return beans == null ? 0 : beans.size();
    }

    @Override
    public DetailListBean getGroup(int groupPosition) {
        return groupList.get(groupPosition);
    }

    @Override
    public ChildCommentsBean getChild(int groupPosition, int childPosition) {
        List<ChildCommentsBean> beans = childMap.get(groupPosition);
        return beans == null ? null : beans.get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        List<ChildCommentsBean> beans = childMap.get(groupPosition);
        return beans == null ? 0 : beans.get(childPosition).hashCode();

    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

        GroupHolder groupHolder;

        if (convertView == null) {

            convertView = inflater.inflate(R.layout.detail_list_group_item, parent, false);

            groupHolder = new GroupHolder();

            groupHolder.userImageView = convertView.findViewById(R.id.userImageView);
            groupHolder.nameTextView = convertView.findViewById(R.id.nameTextView);
            groupHolder.content = convertView.findViewById(R.id.content);
            groupHolder.time = convertView.findViewById(R.id.time);

            convertView.setTag(groupHolder);

        } else {

            groupHolder = (GroupHolder) convertView.getTag();

        }

        DetailListBean group = getGroup(groupPosition);

        UserBean user = group.getUser();

        if (user != null) {

            String user_image_url = context.getResources().getString(R.string.img_url_base);

            user_image_url = user_image_url + user.getImgpath();

            GlideApp.with(context)
                    .load(user_image_url)
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .error(R.drawable.ic_default_image)
                    .centerCrop()
                    .into(groupHolder.userImageView);

            groupHolder.nameTextView.setText(user.getName());

            groupHolder.content.setText(group.getContent());

            groupHolder.time.setText(group.getPost_time());
        }


        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildHolder childHolder;

        if (convertView == null) {

            convertView = inflater.inflate(R.layout.detail_list_children_item, parent, false);

            childHolder = new ChildHolder();

            childHolder.userImageView = convertView.findViewById(R.id.userImageView);
            childHolder.nameTextView = convertView.findViewById(R.id.nameTextView);
            childHolder.content = convertView.findViewById(R.id.content);
            childHolder.time = convertView.findViewById(R.id.time);

            convertView.setTag(childHolder);

        } else {

            childHolder = (ChildHolder) convertView.getTag();

        }

        ChildCommentsBean child = getChild(groupPosition, childPosition);


        if (child != null){

            DetailUserBeanX user = child.getUser();

            String user_image_url = context.getResources().getString(R.string.img_url_base);

            user_image_url = user_image_url + user.getImgpath();

            GlideApp.with(context)
                    .load(user_image_url)
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .error(R.drawable.ic_default_image)
                    .centerCrop()
                    .into(childHolder.userImageView);

            childHolder.nameTextView.setText(user.getName());

            childHolder.content.setText(child.getContent());

            childHolder.time.setText(child.getPost_time());


        }

        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

    class GroupHolder {
        ImageView userImageView;
        TextView nameTextView, content, time;
    }

    class ChildHolder {
        ImageView userImageView;
        TextView nameTextView, content, time;
    }
}
